﻿package mortal.game.view.autoArena.view
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.autoArena.util.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class AutoArenaRewardSendWin extends BaseWindow
    {
        private var _rankTxt:GImageBitmap;
        private var _takeRewardBtn:GLoadingButton;
        private var _itemArr:Array;
        private var _itemSpr:GSprite;
        private static var _instance:AutoArenaRewardSendWin;

        public function AutoArenaRewardSendWin(param1:ILayer = null)
        {
            this._itemArr = [];
            super(param1);
            setSize(343, 290);
            title = Language.getString(40894);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.AutoArenaNewRecordBg, 11, 38, this));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.AutoArenaNewRecordTxt4, 50 - 8, 46, this));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.AutoArenaNewRecordTxt3, 137 + 5, 75 + 2, this));
            this._rankTxt = UIFactory.gImageBitmap("", 214 - 16 - 10 - 15, 46, this);
            this._takeRewardBtn = UIFactory.gLoadingButton(ResFileConst.AutoArenaTakeRewardBtn, 120, 239, 103, 38, this);
            this._takeRewardBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._itemSpr = UICompomentPool.getUICompoment(GSprite);
            this._itemSpr.y = 100 + 36;
            this.addChild(this._itemSpr);
            this.initItemList();
            this.addListeners();
            return;
        }// end function

        private function addListeners() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.AutoArenaGetRewardSucc, this.onTakeRewardSucc);
            return;
        }// end function

        private function removeListeners() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.AutoArenaGetRewardSucc, this.onTakeRewardSucc);
            return;
        }// end function

        private function initItemList() : void
        {
            var _loc_4:int = 0;
            var _loc_5:BaseItem = null;
            var _loc_6:ItemData = null;
            var _loc_1:* = Cache.instance.autoArena.sAutoArenaMyInfo;
            if (_loc_1 == null)
            {
                return;
            }
            var _loc_2:* = _loc_1.rank;
            var _loc_3:* = AutoArenaUtil.getOnlineInfoByRank(1, _loc_2);
            this._rankTxt.imgUrl = "AutoArena_Rank_From" + this.getRankRange(_loc_2);
            if (_loc_3)
            {
                _loc_4 = _loc_3.exp;
                if (_loc_4 > 0)
                {
                    _loc_5 = UICompomentPool.getUICompoment(BaseItem);
                    _loc_5.setItemStyle(ItemStyleConst.Big, ImagesConst.ShopItemBg, 6, 6);
                    this._itemSpr.addChild(_loc_5);
                    this.pushUIToDisposeVec(_loc_5);
                    _loc_5.itemCode = 180011015;
                    this._itemArr.push(_loc_5);
                }
                if (_loc_3.unit1 > 0)
                {
                    _loc_5 = UICompomentPool.getUICompoment(BaseItem);
                    _loc_5.setItemStyle(ItemStyleConst.Big, ImagesConst.ShopItemBg, 6, 6);
                    this._itemSpr.addChild(_loc_5);
                    this.pushUIToDisposeVec(_loc_5);
                    _loc_6 = new ItemData(4);
                    _loc_6.itemAmount = _loc_3.num1 / 100;
                    _loc_5.itemData = _loc_6;
                    this._itemArr.push(_loc_5);
                }
                if (_loc_3.itemCode > 0)
                {
                    _loc_5 = UICompomentPool.getUICompoment(BaseItem);
                    _loc_5.setItemStyle(ItemStyleConst.Big, ImagesConst.ShopItemBg, 6, 6);
                    this._itemSpr.addChild(_loc_5);
                    this.pushUIToDisposeVec(_loc_5);
                    _loc_5.itemCode = _loc_3.itemCode;
                    this._itemArr.push(_loc_5);
                }
            }
            this.setPos();
            return;
        }// end function

        private function getRankRange(param1:int) : int
        {
            var _loc_2:int = 0;
            if (param1 == 1)
            {
                _loc_2 = 1;
            }
            if (param1 >= 2 && param1 <= 5)
            {
                _loc_2 = 2;
            }
            if (param1 >= 6 && param1 <= 10)
            {
                _loc_2 = 6;
            }
            if (param1 >= 11 && param1 <= 50)
            {
                _loc_2 = 11;
            }
            if (param1 >= 51 && param1 <= 100)
            {
                _loc_2 = 51;
            }
            if (param1 >= 101 && param1 <= 500)
            {
                _loc_2 = 101;
            }
            if (param1 >= 501 && param1 <= 1000)
            {
                _loc_2 = 501;
            }
            if (param1 >= 1001)
            {
                _loc_2 = 1001;
            }
            return _loc_2;
        }// end function

        private function setPos() : void
        {
            var _loc_1:int = 0;
            while (_loc_1 < this._itemArr.length)
            {
                
                this._itemArr[_loc_1].x = 78 * _loc_1;
                _loc_1++;
            }
            var _loc_2:* = this._itemArr.length * 78 - 10;
            this._itemSpr.x = (343 - _loc_2) / 2;
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            GameProxy.autoArena.getDayReward();
            return;
        }// end function

        private function onTakeRewardSucc(param1:Object) : void
        {
            var _loc_2:BaseItem = null;
            var _loc_3:Point = null;
            for each (_loc_2 in this._itemArr)
            {
                
                _loc_3 = _loc_2.localToGlobal(new Point(0, 0));
                FlyToNavbarTool.flyToBackPack(_loc_2.bitmapdata, _loc_3);
            }
            this.hide();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl();
            this._rankTxt.dispose();
            this._rankTxt = null;
            this._takeRewardBtn.dispose();
            this._takeRewardBtn = null;
            this._itemSpr.dispose();
            this._itemSpr = null;
            this._itemArr.length = 0;
            this.removeListeners();
            return;
        }// end function

        public static function get instance() : AutoArenaRewardSendWin
        {
            if (_instance == null)
            {
                _instance = new AutoArenaRewardSendWin;
            }
            return _instance;
        }// end function

    }
}
